import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:flutter_bsbdj/data_base/novel_table.dart';
import 'dart:io';
import 'package:flutter/services.dart';

class NovelReader extends StatefulWidget {
  String _bookId;
  String _chapterid;
  String _name;
  bool _lastReader; //是否继续上次阅读
  NovelReader(this._name, this._bookId, this._chapterid, this._lastReader)
      : assert(_bookId != null),
        assert(_chapterid != null);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ReaderWidget();
  }
}

class ReaderWidget extends State<NovelReader> {
  String _chapterid = "0";
  String _loadUrl;
  FlutterWebviewPlugin _flutterWebviewPlugin = FlutterWebviewPlugin();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (Platform.isAndroid) {
      SystemUiOverlayStyle style = SystemUiOverlayStyle();
      SystemChrome.setSystemUIOverlayStyle(style);
      SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    }
    if (widget._lastReader) {
      _getHustoryInfo();
    } else {
      setState(() {
        _loadUrl =
            "http://t.shuqi.com/route.php?pagename=#!/bid/${widget._bookId}/cid/${widget._chapterid}/ct/read";
        _chapterid = widget._chapterid;
      });
    }
    _flutterWebviewPlugin.onUrlChanged.listen((url) {
      print("ReaderWidget");
      print(url);
      if (url.contains("cid") || url.contains("bid")) {
        var sf = url.split("/");
        String cid = sf[sf.length - 3];
        print(cid);
        NovelTable.getInstance().rowUpdata(sf[sf.length - 5], sf[sf.length - 3],
            (t) {
          print(t);
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Container(
        child: _chapterid == "0"
            ? Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      width: 30,
                      height: 30,
                      child: CircularProgressIndicator(),
                    ),
                    Text("正在加载中，请稍等...")
                  ],
                ),
              )
            : Container(
                child: WebviewScaffold(
                  appBar: AppBar(
                    title: Text(widget._name),
                  ),
                  url: _loadUrl,
                ),
              ),
      ),
    );
  }

  void _getHustoryInfo() async {
    var t = await NovelTable.getInstance()
        .getInfoByBid(widget._bookId)
        .then((value) {
      if (value == null) {
        setState(() {
          _loadUrl =
              "http://t.shuqi.com/route.php?pagename=#!/bid/${widget._bookId}/cid/${widget._chapterid}/ct/read";
          _chapterid = widget._chapterid;
          print("value==null");
          print(_loadUrl);
        });
      } else {
        setState(() {
          _loadUrl =
              "http://t.shuqi.com/route.php?pagename=#!/bid/${widget._bookId}/cid/${value.cid}/ct/read";
          _chapterid = value.cid;
          print("value!=null");
          print(_loadUrl);
        });
      }
    });
  }
}
